package mvc.helix;

import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.media.opengl.GL;
import javax.swing.border.TitledBorder;

import org.geom.Vector3D;
import org.geom.curve.ICurve;
import org.geom.render.Engine2D;
import org.geom.render.ViewJOGL;

public class HelixView2D extends ViewJOGL {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static boolean curvePointVisibility = true;

	public static boolean drawCurvePoints = true;

	protected ICurve curve;

	private String planeString;
	
	public HelixView2D(ICurve model, String plane, Dimension d) {
		super();
		this.curve = model;

		this.setPreferredSize(d);
		this.setBorder(new TitledBorder("piano "+plane));
		
		this.planeString = plane;
	}
	
	@Override
	public void render(GL gl) {		
		drawCenter(gl);
		if(curvePointVisibility)
			drawCurvePoints(curve.getPoints(), gl);
	}
	
	public void addMouseController(HelixMouseController2D controller) {
		addMouseListener((MouseListener) controller);
		addMouseMotionListener((MouseMotionListener) controller);
	}

	protected void drawCenter(GL gl) {
		gl.glPushMatrix();
		gl.glScalef(10, 10, 10);

		gl.glBegin(GL.GL_LINE);

		gl.glColor3f(0, 0, 0);
		gl.glVertex3i(0, 0, 0);
		gl.glVertex3i(1, 0, 0);

		gl.glColor3f(0, 0, 0);
		gl.glVertex3i(0, 0, 0);
		gl.glVertex3i(0, 1, 0);

		gl.glEnd();

		gl.glPopMatrix();
		gl.glFlush();
	}
	
	protected void drawCurvePoints(ArrayList<Vector3D> points, GL gl) {
		int k = 2;

		gl.glPushMatrix();

		gl.glColor3f(0.3f,0.3f,0.3f);
		gl.glPointSize(k);
		gl.glBegin(GL.GL_POINT);
		for (Vector3D p : points) {
			
			if (this.planeString==Engine2D.X) 
				gl.glVertex2d(p.y, p.z);
			if (this.planeString==Engine2D.Y) 
				gl.glVertex2d(p.x, p.z);
			if (this.planeString==Engine2D.Z) 
				gl.glVertex2d(p.x, p.y);
		}
		gl.glEnd();
				
		gl.glPopMatrix();
		gl.glFlush();
	}


	
	
}
